package com.sevenyear.excelanalyze.listener.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sevenyear.excelanalyze.common.utils.CalculateUtils;
import com.sevenyear.excelanalyze.entity.PartitionBaseInfo;
import com.sevenyear.excelanalyze.entity.SideSupplyRate;
import com.sevenyear.excelanalyze.service.PartitionBaseInfoService;
import com.sevenyear.excelanalyze.service.SideSupplyRateService;

/**
 * @author ：SevenYear
 * @description：TODO
 * @date ：2021/8/30 14:46
 */
public class SideSupplyRateListener extends AnalysisEventListener<SideSupplyRate> {

    private SideSupplyRateService sideSupplyRateService;
    private PartitionBaseInfoService partitionBaseInfoService;

    public SideSupplyRateListener() {
    }

    public SideSupplyRateListener(SideSupplyRateService sideSupplyRateService, PartitionBaseInfoService partitionBaseInfoService) {
        this.sideSupplyRateService = sideSupplyRateService;
        this.partitionBaseInfoService = partitionBaseInfoService;
    }

    @Override
    public void invoke(SideSupplyRate sideSupplyRate, AnalysisContext analysisContext) {
        //计算平均值
        float average = CalculateUtils.average(sideSupplyRate.getHydraulicGradientMin(), sideSupplyRate.getHydraulicGradientMax());
        sideSupplyRate.setHydraulicGradientAvg(average);
        //设置分区主键id,分区编码partitionCode,区域编码areaCode
        QueryWrapper<PartitionBaseInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("landform",sideSupplyRate.getLandform()).eq("clay_thickness",sideSupplyRate.getClayThickness());
        PartitionBaseInfo partitionBaseInfo = partitionBaseInfoService.getOne(wrapper);
        sideSupplyRate.setPartitionId(partitionBaseInfo.getId());
        sideSupplyRate.setPartitionCode(partitionBaseInfo.getPartitionCode());
        sideSupplyRate.setAreaCode(sideSupplyRate.getArea()+sideSupplyRate.getPartitionCode());
        UpdateWrapper<SideSupplyRate> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("area_code",sideSupplyRate.getAreaCode());
        sideSupplyRateService.saveOrUpdate(sideSupplyRate,updateWrapper);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
